<template>
  <el-dialog title="添加人员" :visible="visible" width="1250px" append-to-body @close="close">
    <el-container class="tab-container">
      <el-aside width="auto" class="aside">
        <el-tree
          ref="tree"
          class="aside-tree"
          :data="treeData"
          node-key="id"
          :default-expanded-keys="cacheTreeExpandedKeys"
          @current-change="handleTreeSelectChange"
          @node-expand="handleNodeExpand"
          @node-collapse="handleNodeCollapse"
        />
      </el-aside>
      <el-main class="main" style="margin:0">
        <collapse-tab class="main-search">
          <el-form
            :inline="true"
            :model="queryCondition"
            label-width="80px"
            @keyup.enter.native="query"
          >
            <!--查询条件区 -->
            <el-form-item label="姓名">
              <p-input v-model="queryCondition.name" class="form-item" />
            </el-form-item>
            <el-form-item label="账号">
              <p-input v-model="queryCondition.account" class="form-item" />
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="queryCondition.ignoreParent">查询全部</el-checkbox>
            </el-form-item>
            <el-form-item style="float:right;">
              <list-button-query :page-code="pageCode" />
            </el-form-item>
            <div class="clearfix" />
          </el-form>
        </collapse-tab>
        <el-card class="main-report">
          <div>
            <el-button
              v-permission="'system:userGroup:permission'"
              type="primary"
              @click="addUser"
            >新增</el-button>

            <div style="clear:both;" />
          </div>
          <div style="margin-top:5px;">
            <div>
              <columns-controller v-model="columnList" :table-key="tableKey" />
            </div>
            <el-table
              v-loading="loading"
              :data="tableData"
              style="width: 100%"
              highlight-current-row
              border
              @sort-change="handleSortChange"
              @current-change="handleRowChange"
              @selection-change="handleSelectionChange"
              @row-dblclick="handleDoubleClickRow"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column
                v-for="(item, index) in showCols"
                :key="index"
                :label="item.label"
                :prop="item.prop"
                :show-overflow-tooltip="item.showOverflowTooltip"
                :width="item.width"
                :formatter="item.formatFunc"
                :sortable="item.sortable"
              />
            </el-table>
          </div>
          <list-pager
            :page-num="pageInfo.pageNum"
            :page-size="pageInfo.pageSize"
            :page-total="pageTotal"
          />
        </el-card>
      </el-main>
    </el-container>
  </el-dialog>
</template>

<script>
import { listMixin } from '@/mixin/treeListMixin'

const MODULE_CODE = 'system'
const ENTITY_TYPE = 'user'
export default {
  name: 'addUser',
  mixins: [listMixin],
  data() {
    return {
      entityType: ENTITY_TYPE,
      moduleCode: MODULE_CODE,
      // eslint-disable-next-line no-eval
      api: eval('this.$api.' + MODULE_CODE + '.' + ENTITY_TYPE),
      apiTree: this.$api.system.organization,
      pageCode: MODULE_CODE + ':' + ENTITY_TYPE + ':',
      queryCondition: {
        parentId: '',
        organizationId: ''
      },
      columnList: [
        // prop label show 必须定义， width/showOverflowTooltip/formatFunc/sortable 需要的话定义
        { prop: 'name', label: '姓名', show: true },
        { prop: 'account', label: '账号', show: true },
        { prop: 'organizationName', label: '组织机构', show: true, showOverflowTooltip: true }

      ],
      treeData: null,
      cacheTreeExpandedKeys: [this.constant.DEFAULT_EXPANDED_ID],
      isRefreshTree: false,
      parentId: '',
      parentName: '',
      visible: false,
      userGroupId: ''

    }
  },
  watch: {
    parentId() {
      this.queryCondition.organizationId = this.parentId
    }
  },
  methods: {
    show(id) {
      this.userGroupId = id
      this.loadData()
      this.visible = true
    },
    addUser() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'info',
          message: '请至少选中一行'
        })
        return
      }
      this.$confirm('确定将所选用户添加到用户组中吗?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.system.userGroup.addUser(this.userGroupId, { idList: this.multipleSelection.map(row => row.id) })
          .then(() => {
            this.$emit('ok')
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    close() {
      this.visible = false
    }

  }
}
</script>

  <style lang="scss">
.tab-container {
  .main .main-search {
    .el-form-item {
      margin-bottom: 5px;
    }
  }
}
</style>
  <style lang="scss" scoped>
.tab-container {
  $item-width: 200px;
  padding: 15px;
  .aside {
    min-width: 180px;
    background-color: white;
  }
  .main {
    .main-search {
      .form-item {
        width: $item-width;
      }
    }
    .main-report {
      margin-top: 10px;
    }
  }
}
</style>
